{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# about Tkinter\n",
    "\n",
    "## 1 understand package\n",
    "\n",
    "\n",
    "遇到新的module，先用三板斧：\n",
    "1. 导入\n",
    "2. 查三项\n",
    "3. 求助\n",
    "4. 如果是package，还要增加一项，检查目录，看有哪些module和subpackage（子包）\n",
    "\n",
    "```\n",
    "cd [包的目录]\n",
    "ls -l\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing aboutTkinter/intro.py\n"
     ]
    }
   ],
   "source": [
    "#%%writefile aboutTkinter/intro.py\n",
    "import tkinter as tk                                 #导入  \n",
    "print(type(tk),tk.__package__,tk.__file__)           #检查三项\n",
    "print(help(tk))                                      #求帮助\n",
    "#%cd /Users/xingyongkang/anaconda2/envs/dgl/lib/python3.6/tkinter/ 查目录\n",
    "#%ls -l\n",
    "\n",
    "import tkinter.messagebox as tb\n",
    "print(type(tb),tb.__package__,tb.__file__)\n",
    "print(type(tk.constants),tk.constants.__package__,tk.constants.__file__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2 component组件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Overwriting aboutTkinter/component.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutTkinter/component.py\n",
    "import tkinter as tk\n",
    "win = tk.Tk()\n",
    "win.title('my first gui')\n",
    "win.geometry('300x300')\n",
    "l1 = tk.Label(win,text = 'hello world', bg ='green')\n",
    "l1.pack()\n",
    "b1 = tk.Button(win, text='hit me', font=('Arial', 12), width=10, height=1)\n",
    "b1.pack()\n",
    "e1 = tk.Entry(win, text='edit me', font=('Arial', 12))\n",
    "e1.pack()\n",
    "lb1 = tk.Listbox(win)\n",
    "lb1.pack() \n",
    "lb1.insert(\"end\", '北京')\n",
    "lb1.insert(\"end\", '上海')\n",
    "lb1.insert(\"end\", '重庆')\n",
    "\n",
    "\n",
    "win.mainloop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing aboutTkinter/event.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutTkinter/event.py\n",
    "import tkinter as tk\n",
    "import tkinter.messagebox as mb\n",
    "\n",
    "win = tk.Tk()\n",
    "win.title('My First GUI')\n",
    "win.geometry('300x300')\n",
    "\n",
    "l1 = tk.Label(win,text = 'hello world', bg ='green')\n",
    "l1.pack()\n",
    "def hit_me():\n",
    "    #mb.showinfo(title='Hi',message='hello')\n",
    "    lb1.insert('end','天津')\n",
    "b1 = tk.Button(win, text='hit me', font=('Arial', 12), width=10, height=1,command = hit_me)\n",
    "b1.pack()\n",
    "\n",
    "\n",
    "e1 = tk.Entry(win, text='edit me', font=('Arial', 12))\n",
    "e1.pack()\n",
    "lb1 = tk.Listbox(win)\n",
    "lb1.pack() \n",
    "lb1.insert(\"end\", '北京')\n",
    "lb1.insert(\"end\", '上海')\n",
    "lb1.insert(\"end\", '重庆')\n",
    "\n",
    "\n",
    "b1.command = hit_me\n",
    "win.mainloop()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing aboutTkinter/menu.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutTkinter/menu.py\n",
    "import tkinter as tk\n",
    "win = tk.Tk()\n",
    "win.title('my first gui')\n",
    "win.geometry('300x300')\n",
    "l1 = tk.Label(win,text = 'hello world', bg ='green')\n",
    "l1.pack()\n",
    "\n",
    "def hit_me():\n",
    "    #mb.showinfo(title='Hi',message='hello')\n",
    "    lb1.insert('end','天津')\n",
    "b1 = tk.Button(win, text='hit me', font=('Arial', 12), width=10, height=1,command = hit_me)\n",
    "b1.pack()\n",
    "e1 = tk.Entry(win, text='edit me', font=('Arial', 12))\n",
    "e1.pack()\n",
    "lb1 = tk.Listbox(win)\n",
    "lb1.pack() \n",
    "lb1.insert(\"end\", '北京')\n",
    "lb1.insert(\"end\", '上海')\n",
    "lb1.insert(\"end\", '重庆')\n",
    "\n",
    "def say_hello():\n",
    "    mb.showinfo(title='Hi',message='hello')\n",
    "\n",
    "menubar = tk.Menu(win)\n",
    "menubar.add_command(label = \"Hello\",command = say_hello)\n",
    "menubar.add_command(label = \"Quit\", command = win.quit)\n",
    "win.config(menu = menubar)\n",
    "\n",
    "\n",
    "\n",
    "# 进入消息循环\n",
    "win.mainloop()\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3 draw\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing aboutTkinter/draw.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile aboutTkinter/draw.py\n",
    "import tkinter as tk\n",
    "win = tk.Tk()\n",
    "win.title('my first gui')\n",
    "win.geometry('300x300')\n",
    "l1 = tk.Label(win,text='draw anything')\n",
    "l1.pack()\n",
    "cv = tk.Canvas(win,bg = 'white')\n",
    "cv.create_rectangle(10,10,200,200)\n",
    "cv.create_line(10,10,200,200)\n",
    "cv.create_oval(10,10,100,100)\n",
    "\n",
    "cv.pack()\n",
    "\n",
    "win.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
